package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.MyThreadLocal;
import com.tanhua.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;


    /**
     * 查询用户通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 设置通知
     * @param map
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){

        settingsService.saveNotifications(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int pagesize){

        PageResult pr = settingsService.blacklist(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消黑名单
     */
    @DeleteMapping("//blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") String uid){
        settingsService.deleteBlackList(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号  验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String phone = MyThreadLocal.getUserMobile();
        settingsService.sendMsg(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 校验验证码
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String code = (String) map.get("verificationCode");
        String phone = MyThreadLocal.getUserMobile();
        Boolean verification =settingsService.checkVerificationCode(phone,code);
        Map hashMap = new HashMap<>();
        hashMap.put("verification",verification);

        return ResponseEntity.ok(hashMap);
    }

    /**
     * 修改手机号 保存
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        settingsService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }

}


